package com.erpanderp.wam.usm.view.projectandassignment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;

import com.erpanderp.wam.adm.model.entity.Role;
import com.erpanderp.wam.usm.model.entity.projectandassignment.Project;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.projectandassignment.ProjectDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeRoleDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;


@ManagedBean (name="projectViewBean")
@ViewScoped
public class ProjectsBean implements Serializable{

	private static final long serialVersionUID = 1L;
	
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
			.getExternalContext().getSession(true);
	
	private List<Project> projects = new ArrayList<Project>();
	private Project project = new Project();
	private Map<String, Long> projectsMap = new HashMap<String, Long>();
	private List<Employee> ownerOfProject = new ArrayList<Employee>();
	private Map<String, Long> ownerOfProjectMap = new HashMap<String, Long>();
	private boolean detailsForProject;
	private Long selectedProjectId;
	
	private Project selectedProject=new Project();
	private Role role=new Role();
	
	private boolean FORM_VALID;
	private Long selectedPMId;
	
	private EmployeeDAO employeeDAO =  new EmployeeDAO();	
	private ProjectDAO projectDAO = new ProjectDAO();
	private EmployeeRoleDAO employeeRoleDAO=new EmployeeRoleDAO();
	
	private WAMUser wamUser;
	private int sno=1;
	private String viewForAcRole;
	
	public ProjectsBean(){
		
		wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
		
		role = employeeRoleDAO.getRoleByEmployeeId(wamUser.getEmployee().getEmployeeId());
		
		viewForAcRole = role.getRole();
		
		setDetailsForProject(false);
		projects = projectDAO.getAllProjects();
		
		for (Project project : projects) {
			//projectsMap.put(project.getProjectName(), project.getProjectId());
			project.setsNo(sno);
			sno++;
		}
		ownerOfProject = employeeDAO.getCompetencyLeads(); 
		for (Employee projectOwner : ownerOfProject) {
			ownerOfProjectMap.put(projectOwner.getFirstName()+" "+projectOwner.getLastName(), projectOwner.getEmployeeId());
		}
		
	}

	public void projectChangeListener(AjaxBehaviorEvent e) {

		if (selectedProjectId == null || selectedProjectId == 0) {
			setDetailsForProject(false);
		} else {
			
			selectedProject = projectDAO.getProjectByProjectID(selectedProjectId);			
			setDetailsForProject(true);
			
		}

	}
	
	public Map<String, Long> getOwnerOfProjectMap() {
		return ownerOfProjectMap;
	}

	public void setOwnerOfProjectMap(Map<String, Long> ownerOfProjectMap) {
		this.ownerOfProjectMap = ownerOfProjectMap;
	}

	public String updateProjectAction() {	
		validateForm();
		if(FORM_VALID){		
			selectedProject.setDateModified(new Date());
			selectedProject.setModifiedBy(wamUser.getEmployee().getFirstName()+" "+wamUser.getEmployee().getLastName());
			projectDAO.updateProject(selectedProject);		
			
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, selectedProject.getProjectName() + " Project Updated successfully.", ""));
			setDetailsForProject(false);
			projects = projectDAO.getAllProjects();
			projectsMap.clear();
			for (Project project : projects) {
				projectsMap.put(project.getProjectName(), project.getProjectId());
			}
		}else{
			
		}	
		
		
		return null;
	}
	
	public void viewOrEditActionListener(ActionEvent e){
		System.out.println("Listener...");
		//selectedProject = (Project) e.getComponent().getAttributes().get("selectedProject");
		 Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		  
		 String projectId = params.get("projectId");
		 long id = Long.parseLong(projectId);
		 
		 selectedProject=projectDAO.getProjectByProjectID(id);
		  
	}
	public void viewActionListener(ActionEvent e){
		System.out.println("Listener...");
		//selectedProject = (Project) e.getComponent().getAttributes().get("selectedProject");
		 Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		  
		 String projectId = params.get("projectId");
		 long id = Long.parseLong(projectId);
		 
		 selectedProject=projectDAO.getProjectByProjectID(id);
		  
	}
	
	public void updateProjectListener(ActionEvent e){
		 System.out.println("INSIDE UPDATE METHOD:::::");
			
		 validateForm();
			
			if(FORM_VALID){
				System.out.println("Updating Employee...");
				selectedProject.setCreateDate(new Date());
				projectDAO.updateProject(selectedProject);	
				
				RequestContext.getCurrentInstance().execute("projectEditDialog.hide();");
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  " Project updated successfully.", ""));
			}else{
				RequestContext.getCurrentInstance().execute("projectEditDialog.show();");			
			}
	}
	
	public void deleteProjectListener(ActionEvent e){
		 System.out.println("INSIDE DELETE METHOD:::::");
			
		 selectedProject.setStatus("INACTIVE");
		 projectDAO.updateProject(selectedProject);	
		 RequestContext.getCurrentInstance().execute("projectEditDialog.hide();");
		 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  " Project deleted successfully.", ""));
			
	}
	public void closeProjectListener(ActionEvent e){
		 System.out.println("INSIDE CLOSE  METHOD:::::");
		 
				RequestContext.getCurrentInstance().execute("projectViewDialog.hide();");
			
	}
	
	private void validateForm(){
		FORM_VALID = true;
		
		if(ValidationUtil.isEmpty(selectedProject.getProjectName(), "Project Name", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(selectedProject.getClient(), "Client Name", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(selectedProject.getClientLocation(), "Client Location", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		
		if(ValidationUtil.isEmpty(selectedProject.getClientAddress(), "Client Address", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(selectedProject.getContactPerson(), "Client Contact Person", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(selectedProject.getContactNumber(), "Client Contact Number", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(selectedProject.getPeriodFrom() == null ? null : selectedProject.getPeriodFrom().toString(), "Project Start Date", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(selectedProject.getPeriodTo() == null ? null : selectedProject.getPeriodTo().toString(), "Project End Date", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		
	
		if(selectedProject.getPeriodTo().before(selectedProject.getPeriodFrom())){
			   FORM_VALID = false;
			   FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN," Project end date should be greater than project start date", ""));
		}
		
		if(ValidationUtil.isEmpty(selectedProject.getNoOfResourecs() == null ? null : selectedProject.getNoOfResourecs().toString(), "No. of Resources", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(selectedProject.getProjectCreatedBy(), "Project Created By", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		System.out.println("Finally FORM_VALID is ::::0000000:"+FORM_VALID);
		
	}
	
	//GENERATE SETTERS AND GETTERS
	
	public boolean isDetailsForProject() {
		return detailsForProject;
	}
	public void setDetailsForProject(boolean detailsForProject) {
		this.detailsForProject = detailsForProject;
	}

	public List<Project> getProjects() {
		return projects;
	}
	public void setProjects(List<Project> projects) {
		this.projects = projects;
	}



	public Map<String, Long> getProjectsMap() {
		return projectsMap;
	}



	public void setProjectsMap(Map<String, Long> projectsMap) {
		this.projectsMap = projectsMap;
	}



	public Long getSelectedProjectId() {
		return selectedProjectId;
	}



	public void setSelectedProjectId(Long selectedProjectId) {
		this.selectedProjectId = selectedProjectId;
	}



	public Project getSelectedProject() {
		return selectedProject;
	}



	public void setSelectedProject(Project selectedProject) {
		this.selectedProject = selectedProject;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public Long getSelectedPMId() {
		return selectedPMId;
	}

	public void setSelectedPMId(Long selectedPMId) {
		this.selectedPMId = selectedPMId;
	}

	public int getSno() {
		return sno;
	}

	public void setSno(int sno) {
		this.sno = sno;
	}

	public String getViewForAcRole() {
		return viewForAcRole;
	}

	public void setViewForAcRole(String viewForAcRole) {
		this.viewForAcRole = viewForAcRole;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

}
